package com.base.mine;

import android.support.v7.widget.LinearLayoutManager;
import android.view.View;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.base.commonlib.base.CBaseBarMvpActivity;
import com.base.commonlib.bean.Partner;
import com.base.mine.adapter.BaseAdapter;
import com.base.mine.adapter.BaseHolder;
import com.base.mine.databinding.ActivityPartnerBinding;
import com.base.mine.presenter.PartnerPresenter;
import com.base.mine.view.PartnerView;


/**
 * Created by weikailiang on 2019/6/11.
 */
@Route(path = "/mine/partner")
public class PartnerActivity extends CBaseBarMvpActivity<PartnerPresenter> implements PartnerView {


    protected PartnerPresenter mPresenter;
    protected ActivityPartnerBinding mBinding;
    private BaseAdapter<Partner> mAdapter;
    @Override
    protected int getLayoutResource() {
        return R.layout.activity_partner;
    }

    @Override
    protected void init() {
        super.init();
        mBinding = (ActivityPartnerBinding) mRootBinding;
        mPresenter = new PartnerPresenter(this,this);
        setBackNavigation();
        setBarTitle("我的搭档");

        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(mContext);
        mBinding.rv.setLayoutManager(linearLayoutManager);

        mBinding.fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mPresenter.setPartner();
                finish();
            }
        });
    }

    @Override
    protected void start() {
        mPresenter.start();
        initRv();
    }

    public void initRv() {
        mAdapter = new BaseAdapter<Partner>(mPresenter.mPartners, mContext) {

            @Override
            public void convert(BaseHolder holder, Partner partner, boolean isHeader, boolean isBottom) {
                TextView name = holder.getView(R.id.name);
                name.setText(partner.getUserName());
                if (mPresenter.mPartners.indexOf(partner) == mPresenter.mCurrentIndex) {
                    holder.getView(R.id.iv_aye).setVisibility(View.VISIBLE);
                } else {
                    holder.getView(R.id.iv_aye).setVisibility(View.INVISIBLE);
                }
            }
        };
        mAdapter.putNormalType(R.layout.item_partner);
        mAdapter.setOnItemClickListener(new BaseAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseHolder holder, int dataPosition, boolean isHeader, boolean isBottom) {
                if (dataPosition == mPresenter.mCurrentIndex) {

                } else {
                    int index = mPresenter.mCurrentIndex;
                    mPresenter.mCurrentIndex = dataPosition;
                    if (index != -1) {
                        mAdapter.notifyItemChanged(index);
                    }
                    mAdapter.notifyItemChanged(dataPosition);
                }
            }
        });
        mBinding.rv.setAdapter(mAdapter);
    }

    @Override
    public void showPartner() {
        mAdapter.notifyDataSetChanged();
    }

    @Override
    public void setCurrentPartner(String name) {
        mBinding.name.setText(name);
    }
}
